<template>
  <div class="d" id="d">
    <div class="d">
      <el-input
        type="text"
        placeholder="请输入"
        v-model.trim="Petinfo.pfstrain"
        @blur="page()"
        style="width: 200px"
      >
      </el-input>
      <el-button @click="dialogFormVisible = true">添加</el-button>
    </div>

    <el-table :data="list" border style="width: 100%">
      <el-table-column fixed prop="pfid" label="ID" width="150">
      </el-table-column>
      <el-table-column prop="pfstrain" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="pfsex" label="性别" width="120"> </el-table-column>
      <el-table-column prop="pfdate" label="时间" width="120">
      </el-table-column>
      <el-table-column prop="pfdepict" label="描述" width="300">
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="loadup(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            style="color: red"
            @click="del(scope.row.pfid)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="num"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- add -->
    <el-dialog title="添加数据" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input
            style="width: 200px"
            v-model="form.pfstrain"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <input type="radio" name="pfsex" value="0" v-model="form.pfsex" /> 男
          <input type="radio" name="pfsex" value="1" v-model="form.pfsex" /> 女
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input style="width: 300px" v-model="form.pfdepict"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- update -->
    <el-dialog title="修改数据" :visible.sync="updialogFormVisible">
      <el-form :model="upform">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input
            style="width: 200px"
            v-model="upform.pfstrain"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <input type="radio" name="pfsex" value="0" v-model="upform.pfsex" />
          男
          <input type="radio" name="pfsex" value="1" v-model="upform.pfsex" />
          女
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input style="width: 300px" v-model="upform.pfdepict"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="up()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.page();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.num = val;
      this.page();
    },
    page() {
      this.axios
        .get("http://localhost:8086/Petinfo.do/cha", {
          params: {
            num: this.num,
            size: this.size,
            petinfo: JSON.stringify(this.Petinfo),
          },
        })
        .then((res) => {
          this.list = res.data.list;
          this.total = res.data.total;
        });
    },

    add() {
      this.axios
        .post("http://localhost:8086/Petinfo.do/add", this.form)
        .then((res) => {
          if (res.data) {
            this.page();
          }
        });
      this.dialogFormVisible = false;
    },
    loadup(res) {
      this.upform = res;
      this.updialogFormVisible = true;
    },
    up() {
      this.axios
        .post("http://localhost:8086/Petinfo.do/update", this.upform)
        .then((res) => {
          if (res.data) {
            this.page();
          }
        });
      this.updialogFormVisible = false;
    },

    del(id) {
      if (confirm("此操作将删除该数据, 是否继续?")) {
        this.axios
          .get("http://localhost:8086/Petinfo.do/delete", {
            params: { id: id },
          })
          .then((res) => {
            if (res.data) {
              this.page();
            }
          });
      }
    },
  },

  data() {
    return {
      num: 1,
      size: 10,
      total: 2,
      list: [],
      Petinfo: { pfstrain: "" },

      dialogFormVisible: false,
      updialogFormVisible: false,
      form: {
        pfstrain: "",
        pfsex: "0",
        pfdepict: "",
      },
      upform: {},
    };
  },
  created() {
    this.page();
  },
 
};
</script>

<style scoped>
.d {
  text-align: left;
}
</style>